package com.awenlchan.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class MusicService extends Service {
    MediaPlayer player = new MediaPlayer();
    private String mp3Path = "";
    private boolean isPause = false;
    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mp3Path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mp3/";
        Log.i("path",mp3Path);
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getStringExtra("action");
        switch(action){
            case "play":
                try {
                    play();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "stop":
                stop();
                break;
            case "pause":
                pause();
                break;
        }
        return super.START_NOT_STICKY;
    }
    private void play()throws Exception{
        if(!isPause){
            player.reset();
            player.setDataSource(mp3Path+"Pressure.mp3");
            player.prepare();
        }
        player.start();

    }
    private void stop(){
        player.stop();
    }
    private void pause(){
        player.pause();
        isPause=true;
    }
}
